Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for Tags on non base level JSON format #4284

Merged
merged 6 commits into from
Jun 14, 2018
Merged

Conversation

maxunt
Copy link
Contributor

@maxunt maxunt commented Jun 13, 2018

Required for all PRs:

  • Signed CLA.
  • Associated README.md updated.
  • Has appropriate unit tests.

@maxunt maxunt force-pushed the bugfix/json-parser branch from cc76d9c to c9269b8 Compare June 14, 2018 00:00
case bool:
tags[name] = strconv.FormatBool(fields[name].(bool))
delete(fields, name)
case int:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not possible to have ints in parsed JSON, so we can remove this case. Let's also add a warning though if we hit the default case:

log.Printf("E! [parsers.json] Unrecognized type %T", value)

for _, name := range p.TagKeys {
//switch any fields in tagkeys into tags
if fields[name] != nil {
switch fields[name].(type) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid needing to type assert in each case, you can do this:

switch value := fields[name].(type) {

Then in each case the value will already be in the type of the case label.

func (p *JSONParser) switchFieldToTag(tags map[string]string, fields map[string]interface{}) (map[string]string, map[string]interface{}) {
for _, name := range p.TagKeys {
//switch any fields in tagkeys into tags
if fields[name] != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, instead of nesting the switch statement in this check, I would check and continue:

if fields[name] == nil {
    continue
}

switch ....

This help with readability because the scope of the conditional is reduced.

@@ -119,6 +159,9 @@ func (f *JSONFlattener) FlattenJSON(
if f.Fields == nil {
f.Fields = make(map[string]interface{})
}

//bool values to be changed to allow strings, bools as fields
//changed to true true, func switchFieldToTag will remove values
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this comment

parser := JSONParser{
MetricName: "json_test",
TagKeys: []string{"total_devices", "total_threads", "shares_tester", "shares_tester3"},
//TagKeys: []string{"mytag", "a", "b_c", "b_d"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove any commented out code

t.Logf("error: %v", err)
t.Logf("num of metrics: %v", len(metrics))
t.Logf("metric tags: %v", metrics[0].Tags())
t.Logf("metric fields: %v", metrics[0].Fields())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of logging the values, use require to assert that the values are as expected.

if err != nil {
return nil, err
}

metric, err := metric.New(p.MetricName, tags, f.Fields, time.Now().UTC())
tags, nFields := p.switchFieldToTag(tags, f.Fields)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we are adjusting the values after flattening, is it still required to keep the loop above on line 47?

@danielnelson danielnelson added this to the 1.7.1 milestone Jun 14, 2018
@danielnelson danielnelson added the fix pr to fix corresponding bug label Jun 14, 2018
@danielnelson
Copy link
Contributor

closes #4260

@danielnelson danielnelson merged commit 8482c40 into master Jun 14, 2018
@danielnelson danielnelson deleted the bugfix/json-parser branch June 14, 2018 20:17
danielnelson pushed a commit that referenced this pull request Jun 14, 2018
mkboudreau added a commit to mkboudreau/telegraf that referenced this pull request Jun 22, 2018
* origin: (39 commits)
  Document path tag in tail input
  Update changelog
  Added path tag to tail input plugin (influxdata#4292)
  Run windows tests with -short
  Fix postfix input handling of multi-level queues (influxdata#4333)
  Update changelog
  Add support for comma in logparser timestamp format (influxdata#4311)
  Update vendoring to dep from gdm (influxdata#4314)
  Update changelog
  Add new measurement with results of pgrep lookup to procstat input (influxdata#4307)
  Update changelog
  Add valuecounter aggregator plugin (influxdata#3523)
  Update changelog
  Update docker input documentation for container status
  Add container status tag to docker input (influxdata#4259)
  Drop CI support for Go 1.8 (influxdata#4309)
  Update changelog
  Fix selection of tags under nested objects in the JSON parser (influxdata#4284)
  Update changelog
  Add owner tag on partitions in burrow input (influxdata#4281)
  ...
codesmith14 pushed a commit to signalfx/telegraf that referenced this pull request Jul 5, 2018
otherpirate pushed a commit to otherpirate/telegraf that referenced this pull request Mar 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix pr to fix corresponding bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants